package com.ruoyi.web.controller.project;

import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProjectUser;
import com.ruoyi.system.service.IProjectUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目人员Controller
 * 
 * @author ruoyi
 * @date 2025-03-11
 */
@RestController
@RequestMapping("/project/user")
@RequiredArgsConstructor
@Api(tags = "项目人员")
public class ProjectUserController extends BaseController{

    private final IProjectUserService projectUserService;


    @GetMapping("/list")
    @ApiOperation("查询项目人员列表")
    public TableDataInfo list(@RequestParam("projectId")Integer projectId)
    {
        startPage();
        List<ProjectUser> list = projectUserService.getList(projectId);
        return getDataTable(list);
    }



    @GetMapping(value = "/{id}")
    @ApiOperation("获取项目人员详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(projectUserService.getById(id));
    }


    @Log(title = "项目人员", businessType = BusinessType.INSERT)
    @ApiOperation("新增项目人员")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ProjectUser projectUser)
    {
        Integer projectId = projectUser.getProjectId();
        projectUser.getUserIdList().forEach(userId->{
            ProjectUser user = new ProjectUser();
            user.setUserId(userId);
            user.setProjectId(projectId);
            projectUserService.save(user);
        });
        return AjaxResult.success();
    }


    @Log(title = "项目人员", businessType = BusinessType.UPDATE)
    @ApiOperation("修改项目人员")
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody ProjectUser projectUser)
    {
        return toAjax(projectUserService.updateById(projectUser));
    }


    @Log(title = "项目人员", businessType = BusinessType.DELETE)
    @ApiOperation("删除项目人员")
	@PostMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(projectUserService.removeBatchByIds(Arrays.asList(ids)));
    }


}
